package com.riant.day03.demo03;
import	java.nio.charset.Charset;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Classname TestFile
 * @Description TODO
 * @Date 2021/3/5 22:00
 * @Created by 张斌
 */
public class TestFile {
    public static void main(String[] args) throws IOException {
        System.out.println(System.getProperty("user.dir"));
        //idea中module下相对路径即为当前的Project下
        File file=new File("day01\\src\\main\\java\\com\\riant\\day03\\demo03\\a.txt");
//        System.out.println(file.getAbsolutePath());
//        System.out.println(file.getPath());
//        System.out.println(file.length());
       // readFileByInt(file);
        readFileByArr(file);
    }

    public static void readFileByArr(File file) {
        try {
            FileInputStream fis = new FileInputStream(file);
            int len=0;
            int count=0;
            byte b[]=new byte[1024];
            StringBuffer stringBuffer=new StringBuffer();
            while((len=fis.read(b))!=-1){
                stringBuffer.append(new String(b,Charset.forName("utf-8")));
                count++;
            }
            System.out.println(stringBuffer);
            System.out.println("count="+count);
            fis.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void  readFileByInt(File file){
        try {
            FileInputStream fis = new FileInputStream(file);
            int len=0;
            int count=0;
            while((len=fis.read())!=-1){
                System.out.print((char)len);
                count++;
            }
            System.out.println("count="+count);
            fis.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
